package com.digitalchina.lxnsy.activity.old;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bigkoo.pickerview.OptionsPickerView;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPest;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.CropTypeVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：byd
 * 创建时间：2017/3/29 13:45
 * 修改备注：
 * ================================================
 */

public class PestDiagnosisActivity extends BaseActivity {

    @Bind(R.id.pest_diagnosis_tv_selected_crops)
    TextView pestDiagnosisTvSelectedCrops;
    @Bind(R.id.pest_diagnosis_layout_et_feature)
    EditText pestDiagnosisLayoutEtFeature;

    private List<CropTypeVO> cropTypeVOList = new ArrayList<>();
    private List<List<CropTypeVO.ListBean>> listLists = new ArrayList<>(); // 二级菜单

    private OptionsPickerView pvOptions;
    private boolean tag = false;

    private String typeCode = "";


    @Override
    public void setLayout() {
        setTitle("病虫害诊断");
        setContentLayout(R.layout.activity_pest_diagnosis);
    }

    @Override
    public void initMember() {
        // 获取农作物分类
        BusinessPest.getCropTypeList(this, mHandler);
    }

    @Override
    public void addListener() {
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MSG.MSG_GET_DISEASE_TYPE_LIST_SUCCESS: // 农作物分类
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_DISEASE_TYPE_LIST_FIELD:
                        LogUtils.e(msg.obj.toString());
                        break;
                    case MSG.MSG_GET_DIAGNOSIS_SUCCESS: // 搜索
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_DIAGNOSIS_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    private void initOptionPicker() {//条件选择器初始化

        if (cropTypeVOList == null || cropTypeVOList.size() == 0)
            return;

        pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                if (null == cropTypeVOList || cropTypeVOList.size() <= 0)
                    return;
                CropTypeVO cropTypeVO = cropTypeVOList.get(options1);
                CropTypeVO.ListBean childList = listLists.get(options1).get(option2);
                typeCode = childList.getCode();
                pestDiagnosisTvSelectedCrops.setText(String.format("%s %s", cropTypeVO.getName(), childList.getName()));
            }
        })
                .setSubmitText("确定")
                .setCancelText("取消")
                .setTitleText("农作物分类")
                .setSubCalSize(16)//确定取消按钮大小
                .setLineSpacingMultiplier(1.7f) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                .setDividerColor(getResources().getColor(R.color.app_color))//设置分割线的颜色
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)//设置滚轮文字大小
                .setSelectOptions(0, 0)  //设置默认选中项
                .build();

        pvOptions.setPicker(cropTypeVOList, listLists);

        if (tag) {
            pvOptions.show();
            tag = false;
        }


    }

    @Override
    public void callBack(Object object) {
        cropTypeVOList = FastJsonUtil.getListBean(object.toString(), "body", "cropTypeList", CropTypeVO.class);

        if (cropTypeVOList == null || cropTypeVOList.size() == 0)
            return;

        for (int i = 0; i < cropTypeVOList.size(); i++) {
            if (cropTypeVOList.get(i).getList() == null || cropTypeVOList.get(i).getList().size() == 0) {
                cropTypeVOList.remove(i);
                i--;
            } else
                listLists.add(cropTypeVOList.get(i).getList());
        }

        initOptionPicker();
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (pvOptions != null && pvOptions.isShowing()) {
                pvOptions.dismiss();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onResume() {
        super.onResume();
        tag = false; // 默认将tag置为false  避免自动弹出选择器
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.pest_diagnosis_layout_select_crops, R.id.pest_diagnosis_layout_immediately})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pest_diagnosis_layout_select_crops:
                showPickCropsView();
                break;
            case R.id.pest_diagnosis_layout_immediately:

                String feature = pestDiagnosisLayoutEtFeature.getText().toString();
                if (StringUtil.isStrEmpty(typeCode)) {
                    showToast("请选择农作物分类");
                    return;
                }
                if (StringUtil.isStrEmpty(feature)) {
                    showToast("请输入病虫害特征");
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("typeCode", typeCode);
                map.put("feature", feature);
                GotoUtil.gotoActivity(PestDiagnosisActivity.this, PestSearchListActivity.class, "map", map);
//                BusinessPest.getDiagnosisList(PestDiagnosisActivity.this, typeCode, feature, mHandler);
                break;
        }
    }

    private void showPickCropsView() {
        if (pvOptions != null) {
            pvOptions.show();
        } else {
            showToast("正在获取农作物分类");
            tag = true;
            BusinessPest.getDiseaseTypeList(PestDiagnosisActivity.this, mHandler);
        }
    }


}
